import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewSwingApp extends javax.swing.JFrame  implements MouseMotionListener, ActionListener, Serializable {

	private JComboBox comboBox;
	/**
	 * 
	 */
	
	private static final int gridStartX = 408;
	private static final int gridStartY = 80;
	private static final long serialVersionUID = 1L;
	private static ArrayList<String> orientations;
	private JButton undoButton;
	private JButton mapButton;
	private JButton saveButton;
	private JPanel gridPanel;
	private JPanel mapPanel;
	private JPanel labelsPanel;
	private JPanel tilesPanel;
	private ArrayList<JLabel> horizontalLabelsUp;
	private ArrayList<JLabel> horizontalLabelsDown;
	private ArrayList<JLabel> verticalLabelsLeft;
	private ArrayList<JLabel> verticalLabelsRight;
	private ArrayList<Color> colors;
	private ArrayList<Team> teams;
	private ArrayList<saveItem> saveItems;
	private int certainOFcount;
	private int potentialOFcount;
	private int teamCount;
	private int prevX;
	private int prevY;
	private int Xindex;
	private int Yindex;
	private int currentTeam;
	private Action lastAction;
	private ArrayList<Image> mapImages;
	private int mapIndex;
	public void setLastAction(Action lastAction) {
		this.lastAction = lastAction;
	}

	private draggableLabel[][] board;
	
	public int getCurrentTeam() {
		return currentTeam;
	}

	/**
	* Auto-generated main method to display this JFrame
	*/

	
	public NewSwingApp(int certainOFcount,int potentialOFcount, int teamCount) {
		super();
		addMouseMotionListener(this);
		orientations = new ArrayList<String>();
		orientations.add("horizontal");
		orientations.add("vertical");
		orientations.add("bottomleft");
		orientations.add("bottomright");
		orientations.add("topleft");
		orientations.add("topright");
		orientations.add("cross");
		
		saveItems = new ArrayList<saveItem>();
		mapImages = new ArrayList<Image>();
		mapImages.add(new ImageIcon("img/mapFinal.png").getImage());
		mapImages.add(new ImageIcon("img/map.png").getImage());
		mapImages.add(new ImageIcon("img/map2.png").getImage());
		mapIndex = 0;
		
		
		initColors();
		this.certainOFcount = certainOFcount;
		this.potentialOFcount = potentialOFcount;
		this.teamCount = teamCount;
		teams = new ArrayList<Team>();
		initTeams(teamCount);
		board = new draggableLabel[30][40];
		
		
		initGUI();
		
	}
	
	public NewSwingApp(ArrayList<saveItem> savedItems,int certain,int potential,int teams) {
		super();
		addMouseMotionListener(this);
		orientations = new ArrayList<String>();
		orientations.add("horizontal");
		orientations.add("vertical");
		orientations.add("bottomleft");
		orientations.add("bottomright");
		orientations.add("topleft");
		orientations.add("topright");
		orientations.add("cross");
		
		saveItems = new ArrayList<saveItem>();
		for(saveItem item: savedItems)
		{
			saveItems.add(item);
		}
		mapImages = new ArrayList<Image>();
		mapImages.add(new ImageIcon("img/mapFinal.png").getImage());
		mapImages.add(new ImageIcon("img/map.png").getImage());
		mapImages.add(new ImageIcon("img/map2.png").getImage());
		mapIndex = 0;
		
		
		initColors();
		this.certainOFcount = certain;
		this.potentialOFcount = potential;
		this.teamCount = teams;
		this.teams = new ArrayList<Team>();
		initTeams(teamCount);
		board = new draggableLabel[30][40];
		
		
		initGUI(savedItems);
		
	}
	private void initColors()
	{
		colors = new ArrayList<Color>();
		colors.add(Color.black);
		colors.add(Color.red);
		colors.add(Color.cyan);
		colors.add(Color.white);
		colors.add(Color.green);
		colors.add(Color.yellow);
		colors.add(Color.gray);
		colors.add(Color.blue);
		colors.add(Color.magenta);
		colors.add(Color.orange);
		colors.add(Color.lightGray);
		colors.add(Color.pink);
		colors.add(Color.DARK_GRAY);
		colors.add(new Color(255,182,193));
		colors.add(new Color(75,0,130));
		colors.add(new Color(61,89,171));
		colors.add(new Color(188,210,238));
		colors.add(new Color(0,191,255));
		colors.add(new Color(0,134,139));
		colors.add(new Color(102,205,170));
		colors.add(new Color(173,255,47));
		colors.add(new Color(205,155,155));
		colors.add(new Color(142,142,56));
		colors.add(new Color(238,180,180));
		colors.add(new Color(238,230,133));
		colors.add(new Color(218,165,32));
		colors.add(new Color(255,193,37));
		colors.add(new Color(135,38,87));
		colors.add(new Color(227,168,105));
		colors.add(new Color(250,128,114));
		colors.add(new Color(250-25,128-25,114-25));
		colors.add(new Color(250-60,128-60,114-60));
		
		
	}
	private String[] getTeamNumbers()
	{
		String[] result = new String[teams.size()];
		int i=0;
		for(Team t:teams)
		{
			result[i]=t.getNumber()+"";
			i++;
		}
		return result;
	}
	private void initRoads()
	{
		int offset = 0;
		int step =	40;
		JLabel title = new JLabel("Roads");
		title.setBounds(100+50, 120, 100, 30);
		tilesPanel.add(title);
		for (String orientation : orientations)
		{
			ImageIcon icon2 = new ImageIcon("img/Road/road_"+orientation+".png");
			ImageIcon icon2Scaled = scaleTo20(icon2);
			Road road = new Road(107+50,160+offset,icon2Scaled,orientation,false);
			tilesPanel.add(road);
			offset+=step;
		}
	}
	
	private void initBridges()
	{
		int offset = 0;
		int step =	40;
		JLabel title = new JLabel("Bridges");
		title.setBounds(100+50+45, 120, 100, 30);
		tilesPanel.add(title);
		for (String orientation : orientations)
		{
			ImageIcon icon2 = new ImageIcon("img/Bridge/bridge_"+orientation+".png");
			ImageIcon icon2Scaled = scaleTo20(icon2);
			Road road = new Road(107+50+50,160+offset,icon2Scaled,orientation,true);
			tilesPanel.add(road);
			offset+=step;
		}
	}
	
	private void initTeams(int numberOfTeams)
	{
		for(int i=0;i<numberOfTeams;i++)	
		{
			Team t = new Team("alpha"+(i+1),(i+1),colors.get(i),true);
			teams.add(t);
			
		}
		
	}
	private void initPipes(int teamNumber,boolean initial)
	{
		Team currentTeam = teams.get(teamNumber-1);
		int offset = 0;
		int step =	40;
		for (String orientation : orientations)
		{
			ImageIcon icon = new ImageIcon("img/Pipes/Yellow/pipe_"+orientation+"_yellow.png");
			ImageIcon iconScaled = scaleTo20(icon);
			if(tilesPanel.getComponentAt(55, 160+offset)!=null)
				tilesPanel.remove(tilesPanel.getComponentAt(55,160+offset));
			tilesPanel.repaint();
			Pipe pipe = new Pipe(55,160+offset,iconScaled,currentTeam,orientation,initial);
			tilesPanel.add(pipe);
			offset+=step;
		}
	}
	
	private void initWaterPipes(int teamNumber)
	{
		Team currentTeam = teams.get(teamNumber-1);
		int offset = 0;
		int step =	40;
		for (String orientation : orientations)
		{
			ImageIcon icon = new ImageIcon("img/Pipes/Yellow/pipe_"+orientation+"_yellow.png");
			ImageIcon iconScaled = scaleTo20(icon);
			if(tilesPanel.getComponentAt(55+50, 160+offset)!=null)
				tilesPanel.remove(tilesPanel.getComponentAt(55+50,160+offset));
			tilesPanel.repaint();
			Pipe pipe = new Pipe(55+50,160+offset,iconScaled,currentTeam,orientation,true);
			tilesPanel.add(pipe);
			offset+=step;
		}
	}
	private void initBuildings(int teamNumber)
	{
		int offset = 50;
		Team currentTeam = teams.get(teamNumber-1);
		ImageIcon icon3 = new ImageIcon("img/NoddingDonkey/nodding_donkey_animated20x20.gif");
		//ImageIcon iconScaled3 = scaleTo20(icon3);
		if(tilesPanel.getComponentAt(170+50+offset, 155)!=null)
			tilesPanel.remove(tilesPanel.getComponentAt(170+50+offset,155));
		tilesPanel.repaint();

		Donkey donkey = new Donkey(170+50+offset,155,icon3,currentTeam,false,true);

		tilesPanel.add(donkey);
		
		ImageIcon icon4 = new ImageIcon("img/NoddingDonkey/floating_donkey_animated20x20.gif");
		//ImageIcon iconScaled3 = scaleTo20(icon3);
		if(tilesPanel.getComponentAt(170+50+offset, 155+40)!=null)
			tilesPanel.remove(tilesPanel.getComponentAt(170+50+offset,155+40));
		tilesPanel.repaint();
		Donkey donkey2 = new Donkey(170+50+offset,155+40,icon4,currentTeam,true,true);
		tilesPanel.add(donkey2);
		
		ImageIcon icon5 = new ImageIcon("img/compound.png");
		ImageIcon iconScaled5 = scaleTo20(icon5);
		if(tilesPanel.getComponentAt(170+50+offset, 155+40+40)!=null)
			tilesPanel.remove(tilesPanel.getComponentAt(170+50+offset,155+40+40));
		tilesPanel.repaint();
		Compound compound = new Compound(170+50+offset,155+40+40,iconScaled5,currentTeam);
		tilesPanel.add(compound);
	}
	public draggableLabel[][] getBoard() {
		return board;
	}

	private void initPotentialOilFields()
	{
		Random randomGenerator = new Random();
		boolean flag = true;
		for(int i=0; i<potentialOFcount;i++)
		{
			
		    int x = randomGenerator.nextInt(39);
		    int y = randomGenerator.nextInt(29);
		    if(x < 2 || x > 37 || y < 2 || y > 27)
		    {
		    	i--;
		    	continue;
		    }
		    if(i % 2 == 0)
		    	flag=true;
		    else
		    	flag=false;
		    
		    System.out.println("X:" + x + " Y:" +y);
		    ImageIcon icon = new ImageIcon("img/OilFields/square_yellow.png");
			ImageIcon iconScaled = scaleTo20(icon);
			OilField oilfield = new OilField(400+x*20+1,50+y*20+1,iconScaled,flag);
			//oilfield.setMoveable(false);
			if(board[y][x] != null)
			{
				i--;
				continue;
			}
			else
			{
				board[y][x] = oilfield;
				tilesPanel.add(oilfield);
				saveItems.add(new saveItem("potential",400+x*20+1,50+y*20+1,x,y,null,false,-1,false,flag,false,false));
			}
			
			
			
		}
	}
	
	private void initCertainOilFields()
	{
		Random randomGenerator = new Random();
		for(int i=0; i<certainOFcount;i++)
		{
			
		    int x = randomGenerator.nextInt(39);
		    int y = randomGenerator.nextInt(29);
		    if(x < 2 || x > 37 || y < 2 || y > 27)
		    {
		    	i--;
		    	continue;
		    }
		    System.out.println("X:" + x + "Y :" +y);
		    ImageIcon icon = new ImageIcon("img/OilFields/square_blue.png");
			ImageIcon iconScaled = scaleTo20(icon);
			OilField oilfield = new OilField(400+x*20+1,50+y*20+1,iconScaled,true);
			//oilfield.setMoveable(false);
			if(board[y][x] != null)
			{
				i--;
				continue;
			}
			else
			{
				board[y][x] = oilfield;
				tilesPanel.add(oilfield);
				saveItems.add(new saveItem("certain",400+x*20+1,50+y*20+1,x,y,null,false,-1,false,true,false,false));
			}
		}
	}
	private void initGUI() {
		try {
			{
				initContents();
				initCertainOilFields();
				initPotentialOilFields();
				for(Team team : teams)
				{
					draggableLabel teamMarker = new draggableLabel(160,60,40,team.getNumber(),team.getColor());
					tilesPanel.add(teamMarker);
					saveItems.add(new saveItem("marker",160,60,-1,-1,null,false,team.getNumber(),false,false,false,false));
				}
			}
			setSize(1280, 760);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void initGridLabels()
	{
		int horizontalX = 400;
		int horizontalY = 30;
		int offsetX = 0;
		for(int i = 1; i <= 40; i++)
		{
			JLabel horizontalLabelUp =  new JLabel(("" + i),JLabel.CENTER);
			JLabel horizontalLabelDown =  new JLabel(("" + i),JLabel.CENTER);
			horizontalLabelUp.setBounds(horizontalX + offsetX, horizontalY, 20, 20);
			horizontalLabelDown.setBounds(horizontalX + offsetX, horizontalY+622, 20, 20);
			horizontalLabelsUp.add(horizontalLabelUp);
			horizontalLabelsDown.add(horizontalLabelDown);
			labelsPanel.add(horizontalLabelUp);
			labelsPanel.add(horizontalLabelDown);
			offsetX += 20;
		}
		
		verticalLabelsLeft = new ArrayList<JLabel>();
		verticalLabelsRight = new ArrayList<JLabel>();
		int verticalX = 376;
		int verticalY = 50;
		int offsetY = 0;
		char c = '@';
		for(int i = 1; i <= 26; i++)
		{
			JLabel verticalLabelLeft =  new JLabel(("" + (char)(c + i)),JLabel.CENTER);
			verticalLabelLeft.setBounds(verticalX, verticalY + offsetY, 20, 20);
			verticalLabelsLeft.add(verticalLabelLeft);
			labelsPanel.add(verticalLabelLeft);
			JLabel verticalLabelRight =  new JLabel(("" + (char)(c + i)),JLabel.CENTER);
			verticalLabelRight.setBounds(verticalX+824, verticalY + offsetY, 20, 20);
			verticalLabelsRight.add(verticalLabelRight);
			labelsPanel.add(verticalLabelRight);
			offsetY += 20;
		}
		
		JLabel horizontalLabel =  new JLabel("AA",JLabel.CENTER);
		horizontalLabel.setBounds(verticalX, verticalY + offsetY, 20, 20);
		verticalLabelsLeft.add(horizontalLabel);
		labelsPanel.add(horizontalLabel);
		JLabel horizontalLabelRight =  new JLabel("AA",JLabel.CENTER);
		horizontalLabelRight.setBounds(verticalX+824, verticalY + offsetY, 20, 20);
		verticalLabelsRight.add(horizontalLabelRight);
		labelsPanel.add(horizontalLabelRight);
		offsetY += 20;
		
		JLabel horizontalLabel1 =  new JLabel("AB",JLabel.CENTER);
		horizontalLabel1.setBounds(verticalX, verticalY + offsetY, 20, 20);
		verticalLabelsLeft.add(horizontalLabel1);
		labelsPanel.add(horizontalLabel1);
		JLabel horizontalLabelRight1 =  new JLabel("AB",JLabel.CENTER);
		horizontalLabelRight1.setBounds(verticalX+824, verticalY + offsetY, 20, 20);
		verticalLabelsRight.add(horizontalLabelRight1);
		labelsPanel.add(horizontalLabelRight1);
		offsetY += 20;
		
		JLabel horizontalLabel2 =  new JLabel("AC",JLabel.CENTER);
		horizontalLabel2.setBounds(verticalX, verticalY + offsetY, 20, 20);
		verticalLabelsLeft.add(horizontalLabel2);
		labelsPanel.add(horizontalLabel2);
		JLabel horizontalLabelRight2 =  new JLabel("AC",JLabel.CENTER);
		horizontalLabelRight2.setBounds(verticalX+824, verticalY + offsetY, 20, 20);
		verticalLabelsRight.add(horizontalLabelRight2);
		labelsPanel.add(horizontalLabelRight2);
		offsetY += 20;
		
		JLabel horizontalLabel3 =  new JLabel("AD",JLabel.CENTER);
		horizontalLabel3.setBounds(verticalX, verticalY + offsetY, 20, 20);
		verticalLabelsLeft.add(horizontalLabel3);
		labelsPanel.add(horizontalLabel3);
		JLabel horizontalLabelRight3 =  new JLabel("AD",JLabel.CENTER);
		horizontalLabelRight3.setBounds(verticalX+824, verticalY + offsetY, 20, 20);
		verticalLabelsRight.add(horizontalLabelRight3);
		labelsPanel.add(horizontalLabelRight3);
	}
	private void initContents()
	{
		getContentPane().setLayout(null);
		horizontalLabelsUp = new ArrayList<JLabel>();
		horizontalLabelsDown = new ArrayList<JLabel>();
		
		
		labelsPanel = new JPanel();
		labelsPanel.setLayout(null);
		labelsPanel.setBounds(0, 0, 1280, 760);
		getContentPane().add(labelsPanel);
		
		tilesPanel = new JPanel();
		tilesPanel.setLayout(null);
		tilesPanel.setBounds(0, 0, 1280, 760);
		getContentPane().add(tilesPanel);

		
		JLabel title = new JLabel("Buildings");
		title.setBounds(150+50+50, 120, 100, 30);
		tilesPanel.add(title);
		
		JLabel title2 = new JLabel("Pipes");
		title2.setBounds(50, 120, 100, 30);
		tilesPanel.add(title2);
		
		JLabel title1 = new JLabel("Water");
		title1.setBounds(50+50, 110, 100, 30);
		tilesPanel.add(title1);
		
		JLabel title3 = new JLabel("Pipes");
		title3.setBounds(50+50, 125, 100, 30);
		tilesPanel.add(title3);
		
		/*for(saveItem item : saveItems)
		{
			if(item.getType().equals("pipe"))
			{
				teams.get(item.getTeamNumber()-1).setInitial(false);
			}
		}*/
		initRoads();
		initBridges();
		initPipes(1,true);
		initBuildings(1);
		drawColor(1);
		initWaterPipes(1);
		initGridLabels();
		
		final Image gridImage = new ImageIcon("img/GridFinal.png").getImage();
		
		mapPanel = new JPanel(){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void paintComponent(Graphics g) {
			    g.drawImage(mapImages.get(0), 0, 0, mapImages.get(0).getWidth(this),mapImages.get(0).getHeight(this),this );
			  }
		};
		mapPanel.setLayout(null);
		mapPanel.setBounds(400, 50, 802, 602);
		
		gridPanel = new JPanel(){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void paintComponent(Graphics g) {
			    g.drawImage(gridImage, 0, 0, gridImage.getWidth(this),gridImage.getHeight(this),this );
			  }
		};
		gridPanel.setLayout(null);
		gridPanel.setBounds(400, 50, 802, 602);
		
	
		gridPanel.setVisible(true);
		
		
		getContentPane().add(gridPanel);
		getContentPane().add(mapPanel);
		labelsPanel.setOpaque(false);
		gridPanel.setOpaque(false);
		tilesPanel.setOpaque(false);
		mapPanel.setOpaque(false);
		
		getContentPane().setComponentZOrder(mapPanel, 3);
		getContentPane().setComponentZOrder(gridPanel, 2);
		getContentPane().setComponentZOrder(labelsPanel, 0);
		getContentPane().setComponentZOrder(tilesPanel, 1);

		undoButton = new JButton();
		undoButton.setText("Undo");
		undoButton.setBounds(50, 473, 63, 26);
		undoButton.addActionListener(this);
		tilesPanel.add(undoButton);
		
		mapButton = new JButton();
		mapButton.setText("Change Map Background");
		mapButton.setBounds(1000, 675, 200, 26);
		mapButton.addActionListener(this);
		tilesPanel.add(mapButton);
		
		saveButton = new JButton();
		saveButton.setText("Save Game");
		saveButton.setBounds(700, 675, 150, 26);
		saveButton.addActionListener(this);
		tilesPanel.add(saveButton);
		
		
		comboBox = new JComboBox(getTeamNumbers());
		comboBox.setBounds(90, 30, 66, 25);
		comboBox.addActionListener(this);
		getContentPane().add(comboBox);

		final JLabel teamLabel = new JLabel();
		teamLabel.setText("Team:");
		teamLabel.setBounds(50, 30, 66, 16);
		getContentPane().add(teamLabel);
		
		final JLabel colorLabel = new JLabel();
		colorLabel.setText("Color:");
		colorLabel.setBounds(50, 70, 66, 16);
		getContentPane().add(colorLabel);
		
	}
	private void initGUI(ArrayList<saveItem> savedItems) {
		try {
			{
				initContents();
				for(saveItem item : savedItems)
				{
					if(item.getType().equals("marker"))
					{
						draggableLabel teamMarker = new draggableLabel(item.getXPosition(),item.getYPosition(),40,item.getTeamNumber(),teams.get(item.getTeamNumber()-1).getColor());
						tilesPanel.add(teamMarker);
					}
					if(item.getType().equals("pipe"))
					{
						ImageIcon icon = new ImageIcon("img/Pipes/Yellow/pipe_"+item.getOrientation()+"_yellow.png");
						ImageIcon iconScaled = scaleTo20(icon);
						Pipe newPipe = new Pipe(item.getXPosition(),item.getYPosition(),iconScaled,teams.get(item.getTeamNumber()-1),item.getOrientation(),item.isInitial());
						board[item.getYIndex()][item.getXIndex()] = newPipe;
						newPipe.setMoveable(false);
						tilesPanel.add(newPipe);
					}
					if(item.getType().equals("road"))
					{
						ImageIcon icon2;
						if(!item.isBridge())
							icon2 = new ImageIcon("img/Road/road_"+item.getOrientation()+".png");
						else
							icon2 = new ImageIcon("img/Bridge/bridge_"+item.getOrientation()+".png");
						ImageIcon icon2Scaled = scaleTo20(icon2);
						Road road = new Road(item.getXPosition(),item.getYPosition(),icon2Scaled,item.getOrientation(),item.isBridge());
						board[item.getYIndex()][item.getXIndex()] = road;
						road.setMoveable(false);
						tilesPanel.add(road);
					}
					if(item.getType().equals("donkey"))
					{
						ImageIcon icon3;
						if(item.isActive())
						{
							if(!item.isFloating())
							{
								icon3 = new ImageIcon("img/NoddingDonkey/nodding_donkey_animated20x20.gif");
							}
							else
								icon3 = new ImageIcon("img/NoddingDonkey/floating_donkey_animated20x20.gif");
						}
						else
						{
							if(!item.isFloating())
							{
								icon3 = new ImageIcon("img/NoddingDonkey/nodding_donkey.png");
							}
							else
							{
								icon3 = new ImageIcon("img/NoddingDonkey/floating_donkey.png");
							}
						}
				
						board[item.getYIndex()][item.getXIndex()].getParent().remove(board[item.getYIndex()][item.getXIndex()]);
						Donkey newDonkey = new Donkey(item.getXPosition(),item.getYPosition(),icon3,teams.get(item.getTeamNumber()-1),item.isFloating(),item.isActive());
						board[item.getYIndex()][item.getXIndex()]=newDonkey;
						newDonkey.setMoveable(false);
						tilesPanel.add(newDonkey);
					}
					if(item.getType().equals("certain"))
					{
						System.out.println("HERE:"+item.getYIndex()+":"+item.getXIndex());
						ImageIcon icon = new ImageIcon("img/OilFields/square_blue.png");
						ImageIcon iconScaled = scaleTo20(icon);
						OilField oilfield = new OilField(item.getXPosition(),item.getYPosition(),iconScaled,item.isHasOil());
						board[item.getYIndex()][item.getXIndex()]=oilfield;
						//oilfield.setMoveable(false);
						tilesPanel.add(oilfield);
					}
					if(item.getType().equals("potential"))
					{
						System.out.println("HERE:"+item.getYIndex()+":"+item.getXIndex());
						ImageIcon icon = new ImageIcon("img/OilFields/square_yellow.png");
						ImageIcon iconScaled = scaleTo20(icon);
						OilField oilfield = new OilField(item.getXPosition(),item.getYPosition(),iconScaled,item.isHasOil());
						board[item.getYIndex()][item.getXIndex()]=oilfield;
						//oilfield.setMoveable(false);
						tilesPanel.add(oilfield);
					}
					if(item.getType().equals("compound"))
					{
						board[item.getYIndex()][item.getXIndex()].getParent().remove(board[item.getYIndex()][item.getXIndex()]);
						ImageIcon icon5 = new ImageIcon("img/compound.png");
						ImageIcon iconScaled5 = scaleTo20(icon5);
						Compound newCompound = new Compound(item.getXPosition(),item.getYPosition(),iconScaled5,teams.get(item.getTeamNumber()-1));
						board[item.getYIndex()][item.getXIndex()]=newCompound;
						newCompound.setMoveable(false);
						tilesPanel.add(newCompound);
					}
				}
			}
			setSize(1280, 760);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public JPanel getMapPanel() {
		return mapPanel;
	}

	public JPanel getTilesPanel() {
		return tilesPanel;
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		
		
		horizontalLabelsUp.get(prevX).setForeground(Color.black);
		horizontalLabelsDown.get(prevX).setForeground(Color.black);
		horizontalLabelsUp.get(prevX).setBorder(null);
		horizontalLabelsDown.get(prevX).setBorder(null);
		verticalLabelsLeft.get(prevY).setForeground(Color.black);
		verticalLabelsLeft.get(prevY).setBorder(null);
		verticalLabelsRight.get(prevY).setForeground(Color.black);
		verticalLabelsRight.get(prevY).setBorder(null);
		// TODO Auto-generated method stub
		int mouseX = e.getX();
		int mouseY = e.getY();
		Xindex = (mouseX-gridStartX)/20;
		Yindex = (mouseY-gridStartY)/20;
		if(mouseX>=gridStartX && mouseY>=gridStartY)
		{
		
		if(Xindex>=0 && Yindex>=0 && Xindex < 40 && Yindex <30)
		{
			//gridPanel.setVisible(true);
			
			prevX=Xindex;
			prevY=Yindex;
			Border b = BorderFactory.createLineBorder(Color.red);
			horizontalLabelsUp.get(Xindex).setForeground(Color.red);
			horizontalLabelsDown.get(Xindex).setForeground(Color.red);
			verticalLabelsLeft.get(Yindex).setForeground(Color.red);
			verticalLabelsRight.get(Yindex).setForeground(Color.red);
			horizontalLabelsUp.get(Xindex).setBorder(b);
			horizontalLabelsDown.get(Xindex).setBorder(b);
			verticalLabelsLeft.get(Yindex).setBorder(b);
			verticalLabelsRight.get(Yindex).setBorder(b);
		}
		//else
		//{
		//	gridPanel.setVisible(false);
		//}

		}
		//else
		//{
		//	gridPanel.setVisible(false);
		//}
		labelsPanel.repaint();
		System.out.println(mouseX+":"+mouseY+":"+Xindex+":"+Yindex);
		
	}
	public JPanel getGridPanel() {
		return gridPanel;
	}

	public void createNewTile()
	{
	}
	public JPanel getLabelsPanel() {
		return labelsPanel;
	}

	public ArrayList<JLabel> getHorizontalLabelsUp() {
		return horizontalLabelsUp;
	}
	public ArrayList<JLabel> getHorizontalLabelsDown() {
		return horizontalLabelsDown;
	}

	public ArrayList<JLabel> getVerticalLabelsLeft() {
		return verticalLabelsLeft;
	}
	public ArrayList<JLabel> getVerticalLabelsRight() {
		return verticalLabelsRight;
	}

	public int getPrevX() {
		return prevX;
	}

	public int getPrevY() {
		return prevY;
	}

	public int getXindex()
	{
		return Xindex;
	}
	public int getYindex()
	{
		return Yindex;
	}
	public static ImageIcon scaleTo20 (ImageIcon img)
	{
		BufferedImage resizedImage = new BufferedImage(20, 20, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = resizedImage.createGraphics();
		g.drawImage(img.getImage(), 0, 0, 20, 20, null);
		g.dispose();
		g.setComposite(AlphaComposite.Src);
		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		return new ImageIcon(resizedImage);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==comboBox)
		{
			JComboBox cb = (JComboBox)e.getSource();
	        String selected = (String)cb.getSelectedItem();
	        int teamNumber = Integer.parseInt(selected);
	        currentTeam = teamNumber;
	        initPipes(currentTeam,teams.get(currentTeam-1).isInitial());
	        initWaterPipes(currentTeam);
	        initBuildings(currentTeam);
	        drawColor(currentTeam);

		}
		if(e.getSource()==undoButton)
		{
			this.tilesPanel.remove(lastAction.getPlacedTile());
			if(lastAction.hasReplacedTile())
				{
					this.tilesPanel.add(lastAction.getReplacedTile());
					if(lastAction.getReplacedTile() instanceof OilField)
					board[lastAction.getPlacedTile().getYindex()][lastAction.getPlacedTile().getXindex()] = (OilField) lastAction.getReplacedTile();
					if(lastAction.getReplacedTile() instanceof Donkey)
						board[lastAction.getPlacedTile().getYindex()][lastAction.getPlacedTile().getXindex()] = (Donkey) lastAction.getReplacedTile();
				
				}
			else
			{
				board[lastAction.getPlacedTile().getYindex()][lastAction.getPlacedTile().getXindex()] = null;
			}
			this.tilesPanel.repaint();
			
		}
		if(e.getSource() == mapButton)
		{
			mapIndex ++;
			if(mapIndex == mapImages.size())
				mapIndex = 0;
			final Image mapImage1 = mapImages.get(mapIndex);
			mapPanel = new JPanel(){
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				public void paintComponent(Graphics g) {
				    g.drawImage(mapImage1, 0, 0, mapImage1.getWidth(this),mapImage1.getHeight(this),this );
				  }
			};
			
			mapPanel.setLayout(null);
			mapPanel.setBounds(400, 50, 802, 602);
			getContentPane().add(mapPanel);
			mapPanel.setOpaque(false);
			
			getContentPane().setComponentZOrder(mapPanel, 3);
			getContentPane().setComponentZOrder(gridPanel, 2);
			getContentPane().setComponentZOrder(labelsPanel, 1);
			getContentPane().setComponentZOrder(tilesPanel, 0);
			mapPanel.repaint();
		}
		if(e.getSource() == saveButton)
		{
			 try {
				ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("saved.txt"));
				outputStream.write(certainOFcount);
				outputStream.write(potentialOFcount);
				outputStream.write(teamCount);
				outputStream.write(saveItems.size());
				for(saveItem i: saveItems)
				{
					outputStream.writeObject(i);
				}
				//outputStream.write();
				outputStream.flush();
                outputStream.close();
			 } catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		 	 catch (NotSerializableException e2) {
				// TODO Auto-generated catch block
				//e2.printStackTrace();
		 	 } catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
        
	}
	public ArrayList<saveItem> getSaveItems() {
		return saveItems;
	}

	public void drawColor(int teamNumber)
	{
		Team currentTeam = teams.get(teamNumber-1);
		
		if(tilesPanel.getComponentAt(100, 60)!=null)
			tilesPanel.remove(tilesPanel.getComponentAt(100,60));
		tilesPanel.repaint(); 
		Square square = new Square(currentTeam.getColor());
		square.setBounds(100, 60, 40, 40);
		tilesPanel.add(square);
		
	}
	public int getSaveItem(int x,int y)
	{
		int i=0;
		for(saveItem item : saveItems)
		{
			if(item.getXPosition()==x && item.getYPosition()==y)
				{
					return i;
				}
			i++;
		}
		return -1;
	}
	public int getSaveItem(int x,int y,int teamNumer)
	{
		int i=0;
		for(saveItem item : saveItems)
		{
			if(item.getXPosition()==x && item.getYPosition()==y && item.getTeamNumber()==teamNumer)
				{
					return i;
				}
			i++;
		}
		return -1;
	}
}
